package pay

import (
	"context"
	"fmt"
	"github.com/zeromicro/go-zero/core/stores/cache"
	"github.com/zeromicro/go-zero/core/stores/sqlx"
)

var _ Epay18DepositFlowModel = (*customEpay18DepositFlowModel)(nil)

type (
	// Epay18DepositFlowModel is an interface to be customized, add more methods here,
	// and implement the added methods in customEpay18DepositFlowModel.
	Epay18DepositFlowModel interface {
		epay18DepositFlowModel
		UpdateDepositResult(ctx context.Context, flowNo int64, tradeStatus string, actualAmount int64) error
		CountDepositFlow(ctx context.Context, walletFlowNo int64, tradeStatus string) (int64, error)
	}

	customEpay18DepositFlowModel struct {
		*defaultEpay18DepositFlowModel
	}
)

// NewEpay18DepositFlowModel returns a model for the database table.
func NewEpay18DepositFlowModel(conn sqlx.SqlConn, c cache.CacheConf, opts ...cache.Option) Epay18DepositFlowModel {
	return &customEpay18DepositFlowModel{
		defaultEpay18DepositFlowModel: newEpay18DepositFlowModel(conn, c, opts...),
	}
}

func (m *customEpay18DepositFlowModel) UpdateDepositResult(ctx context.Context, flowNo int64, tradeStatus string, actualAmount int64) error {
	q := fmt.Sprintf("update %s set trade_status = ?, actual_amount = ? where flow_no = ?", m.table)
	_, err := m.ExecNoCacheCtx(ctx, q, tradeStatus, actualAmount, flowNo)
	return err
}

func (m *customEpay18DepositFlowModel) CountDepositFlow(ctx context.Context, walletFlowNo int64, tradeStatus string) (int64, error) {
	q := fmt.Sprintf("select count(flow_no) from %s where flow_no = ? and trade_status = ?", m.table)
	var count int64
	err := m.QueryRowNoCacheCtx(ctx, &count, q, walletFlowNo, tradeStatus)
	return count, err
}
